﻿using Abp.Authorization;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Mt.Site.Application.Dashboards.Dto;
using Mt.Site.Core.Base.Authorization.Level;
using Mt.Site.Core.Base.Authorization.Users;
using Mt.Site.Core.ContentManagement.Entities;
//using Mt.Site.Core.FinancialProducts;
using Mt.Site.Core.Navigations.Entities;
//using Mt.Site.Core.Recruitments;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Application.Dashboards
{
    [AbpAuthorize]
    public class DashboardAppService: MtSiteAppServiceBase,IDashboardAppService
    {
        private readonly IRepository<Menu, Guid> _menuRepository;
        private readonly IRepository<Content, Guid> _contentRepository;

        //private readonly IRepository<FinancialProduct,Guid> _financialProductRepository;
        //private readonly IRepository<Recruitment, Guid> _recruitmentRepository;
        public DashboardAppService(IRepository<Menu, Guid> menuRepository,
            IRepository<Content, Guid> contentRepository//,
            //IRepository<FinancialProduct, Guid> financialProductRepository,
            //IRepository<Recruitment, Guid> recruitmentRepository
            )
        {
            _menuRepository = menuRepository;
            _contentRepository = contentRepository;
            //_financialProductRepository = financialProductRepository;
            //_recruitmentRepository = recruitmentRepository;
        }


        public async Task<GetAllDashboardDataDto> GetAllDashboardDataDto()
        {
            return new GetAllDashboardDataDto
            {
                MenuData = await GetContentMenuDisplayData(),
                ContentData = await GetContentDisplayData(),
                FinanicalProductData =  new DisplayDataDto(),
                RecruitmentData = new DisplayDataDto(),
                UserData = await GetUserDisplayData()
            };
        }

        private async Task<DisplayDataDto> GetContentMenuDisplayData()
        {
            var menuQuery = from m in _menuRepository.GetAll()
                       select new
                       {
                           IsActive = m.IsActive,
                           Id = m.Id
                       };
            var list = await menuQuery.ToListAsync();

            return new DisplayDataDto
            {
                ActiveNum = list.Where(item => item.IsActive).Count(),
                UnActiveNum = list.Where(item => !item.IsActive).Count()
            };
        }


        private async Task<DisplayDataDto> GetContentDisplayData()
        {
            var activeContentQuery = from menu in _menuRepository.GetAll().Where(item=>item.IsActive)
                               join c in _contentRepository.GetAll().Where(item=>item.IsActive)
                               on menu.AuxiliaryId equals c.MenuId
                               select new
                               {
                                   Id = c.Id,
                               };
            var activeCount = await activeContentQuery.CountAsync();

            var unActiveQuery = from menu in _menuRepository.GetAll().Where(item => item.IsActive)
                                join c in _contentRepository.GetAll().Where(item => !item.IsActive)
                                on menu.AuxiliaryId equals c.MenuId
                                select new
                                {
                                    Id = c.Id,
                                };
            var unActiveCount = await unActiveQuery.CountAsync();
            return new DisplayDataDto
            {
                ActiveNum = activeCount,
                UnActiveNum = unActiveCount
            };
        }

        //private async Task<DisplayDataDto> GetFinanicalProductDisplayData()
        //{
        //    var productQuery = from menu in _menuRepository.GetAll().Where(item => item.IsActive)
        //                       join p in _financialProductRepository.GetAll()
        //                       on menu.AuxiliaryId equals p.MenuId
        //                       select new
        //                       {
        //                           IsActive = p.IsActive,
        //                           Id = p.Id
        //                       };
        //    var list = await productQuery.ToListAsync();
        //    return new DisplayDataDto
        //    {
        //        ActiveNum = list.Where(item => item.IsActive).Count(),
        //        UnActiveNum = list.Where(item => !item.IsActive).Count()
        //    };
        //}

        //private async Task<DisplayDataDto> GetRecruitmentDisplayData()
        //{
        //    var recruitQuery = 
        //                       from menu in _menuRepository.GetAll()
        //                       join r in _recruitmentRepository.GetAll()
        //                       on menu.AuxiliaryId equals r.MenuId
        //                       select new
        //                       {
        //                           IsActive = r.IsActive,
        //                           Id = r.Id
        //                       };
        //    var list = await recruitQuery.ToListAsync();
        //    return new DisplayDataDto
        //    {
        //        ActiveNum = list.Where(item => item.IsActive).Count(),
        //        UnActiveNum = list.Where(item => !item.IsActive).Count()
        //    };
        //}

        private async Task<DisplayDataDto> GetUserDisplayData()
        {
            var adminWeight = Convert.ToInt32(LevelEnum.Manager);
            var userQuery = from u in UserManager.Users.Where(item=>item.AdministratorWeight < adminWeight)
                            select new
                            {
                                IsActive = u.IsActive,
                                Id = u.Id
                            };
            var list = await userQuery.ToListAsync();
            return new DisplayDataDto
            {
                ActiveNum = list.Where(item => item.IsActive).Count(),
                UnActiveNum = list.Where(item => !item.IsActive).Count()
            };
        }


    }
}
